package com.atguigu.jxc.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.atguigu.jxc.service.UserService;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wswdkl
 * @version 1.0
 * @date 2022/2/12 9:55
 */
@Controller
@RequestMapping("damageListGoods")
public class DamageListGoodsController {

    @Autowired
    private DamageListGoodsService damageListGoodsService;
    @Autowired
    private HttpSession session;

    /**
     * 添加报损单
     * @param damageList
     * @param damageListGoodsStr
     * @param session
     * @return
     */
    @ResponseBody
    @PostMapping("save")
    public ServiceVO saveDamageListGoods(DamageList damageList, String damageListGoodsStr,HttpSession session) {
        //获取userId
        User user = (User)session.getAttribute("currentUser");
        Integer userId = user.getUserId();
        // System.out.println("userId = " + userId);
        damageList.setUserId(userId);
        boolean flag = damageListGoodsService.saveDamageListGoods(damageList,damageListGoodsStr);
        if(flag) {
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } else {
            return new ServiceVO(-1,"请求失败");
        }
    }

    /**
     * 查询报损单
     * @param sTime
     * @param eTime
     * @return
     */
    @ResponseBody
    @PostMapping("list")
    public Map<String,Object> queryDamageList(String sTime, String eTime) {
        return damageListGoodsService.queryDamageList(sTime,eTime);
    }

    /**
     * 查询报损单商品信息
     * @param damageListId
     * @return
     */
    @ResponseBody
    @PostMapping("goodsList")
    public Map<String,Object> queryDamageGoodsList(Integer damageListId) {
        return damageListGoodsService.queryDamageGoodsList(damageListId);
    }


}
